﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;

namespace DAL
{
    public class PersonalInformationService
    {
        /// <summary>
        /// 添加个人信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns>返回已添加的个人信息</returns>
        public static PersonalInformation AddPersonalInformation(PersonalInformation info)
        {
            string sql = string.Format("insert into PersonalInformation(Gender,Name,Nickname,Email, Password,Phone,RegisterTime)"
                                            + "values({0},N'{1}',N'{2}','{3}','{4}','{5}',getdate());select  @@IDENTITY",
                                            info.Gender, info.Name, info.Nickname, info.Email, info.Password, info.Phone);
            int id = DBHelper.GetScalar(sql);
            return GetPersonalInformationById(id);
        }
        /// <summary>
        /// 通过Id获取个人信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static PersonalInformation GetPersonalInformationById(int id)
        {
            string sql = string.Format("select * from PersonalInformation where Id={0}", id);
            return GetPersonalInformationBySql(sql);
        }
        /// <summary>
        /// 通过sql语句获取个人信息
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private static PersonalInformation GetPersonalInformationBySql(string sql)
        {
            List<PersonalInformation> list = GetPersonalInformationsBySql(sql);
            if (list.Count() > 0)
                return list[0];
            return null;
        }
        /// <summary>
        /// 通过sql语句获取个人信息集合
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        private static List<PersonalInformation> GetPersonalInformationsBySql(string sql)
        {
            List<PersonalInformation> list = new List<PersonalInformation>();
            DataTable dt = DBHelper.GetDataSet(sql);
            foreach (DataRow row in dt.Rows)
            {
                PersonalInformation info = new PersonalInformation();
                info.Id = (int)row["Id"];
                info.Name = (string)row["Name"];
                info.Nickname = (string)row["Nickname"];
                info.Password = (string)row["Password"];
                info.Phone = (string)row["Phone"];
                info.Email = (string)row["Email"];
                info.Gender = (int)row["Gender"];
                info.RegisterTime = (DateTime)row["RegisterTime"];
                list.Add(info);
            }
            return list;
        }

        
        /// <summary>
        /// 通过sql语句增加个人信息
        /// </summary>
        /// <param name="sql"></param>
        /// <returns>返回新增加的信息Id</returns>
        private static int AddPersonalInformationBySql(string sql)
        {
            return DBHelper.ExecuteCommand(sql);
        }


        public static PersonalInformation ModifyPersonalInformation(PersonalInformation info)
        {
            string sql = string.Format("update PersonalInformation set "
                                + "Name=N'{0}', Nickname=N'{1}', Password=N'{2}', Email=N'{3}',"
                                + "Phone=N'{4}'"
                                + "where Id={5}",
                                info.Name, info.Nickname, info.Password, info.Email, info.Phone, info.Id);
            DBHelper.ExecuteCommand(sql);
            return GetPersonalInformationById(info.Id);
        }

        public static PersonalInformation GetPersonalInformationByNickname(string nickname)
        {
            string sql = string.Format("select * from PersonalInformation where Nickname=N'{0}'", nickname);
            return GetPersonalInformationBySql(sql);
        }
    }
}
